package com.moleq.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.moleq.device.DeviceManager;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.mgpos.activities.MixmatchSettingActivity;
import com.moleq.models.MixMatch;
import com.moleq.mgpos.activities.R;
import com.moleq.service.MixMatchService;
import com.moleq.service.ServiceManager;

public class MixMatchItemAdapter extends BaseAdapter {
	
	private LayoutInflater mInflater;
	private List<MixMatch> mmlist;
	public ArrayList<CheckBox> cbs = new ArrayList<CheckBox>();
	public boolean lb_check;
	private int selectedRow = 0;
	public MixmatchSettingActivity fcsa;
	private MixMatchService mmService;
	private View ImageButtonView = null;
	private MessageDialog mMessageDialog;
	private DeviceManager mDeviceManager;
	public MixMatchItemAdapter(Context context,List<MixMatch> mmlist){
		this.mInflater = LayoutInflater.from(context);
		this.mmlist = mmlist;
		
		fcsa = (MixmatchSettingActivity) context;
		mmService = ServiceManager.getInstance().mMixMatchService;
		mDeviceManager = DeviceManager.getInstance();
	}
	
	public void setSource(List<MixMatch> mmlist){
		this.mmlist = mmlist;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return mmlist.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mmlist.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parents) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		String upc,name;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.mm_item_list, null);
			holder.tv_item = (TextView) convertView.findViewById(R.id.tv_upc);
			holder.tv_des = (TextView) convertView.findViewById(R.id.tv_name);
			holder.ibtn_delete = (ImageButton) convertView.findViewById(R.id.ibtn_delete);
			holder.ll_delete = (LinearLayout) convertView.findViewById(R.id.ll_delete);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		 upc = mmlist.get(position).getUpc();
		name = mmService.getItemname(upc);


			holder.tv_item.setText(String.valueOf(upc));
			if (name.length() > 11) {
				holder.tv_des.setText(String.valueOf(name.substring(0,12)));
			}else {
				holder.tv_des.setText(String.valueOf(name));
			}
			holder.ibtn_delete.setTag(String.valueOf(mmlist.get(position).getUpc()));
		
		if (position == selectedRow) {
		holder.tv_item.setBackgroundColor(Color.rgb(255,219,16));
		holder.tv_des.setBackgroundColor(Color.rgb(255,219,16));
		holder.ll_delete.setBackgroundColor(Color.rgb(255, 219, 16));
		} else {
			holder.tv_item.setBackgroundColor(Color.rgb(204, 204, 204));
			holder.tv_des.setBackgroundColor(Color.rgb(204, 204, 204));
			holder.ll_delete.setBackgroundColor(Color.rgb(204, 204, 204));
		}
		
			holder.ibtn_delete.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					ImageButtonView = v;
						ShowMessage(MessageDialog.MESSAGE_BOX_YES_NO,MessageDialog.ICON_INFO,"Are you sure to remove the item from the mix match group?");
				}
			});
		return convertView;
	}
	
//	public OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {
//		
//		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//			// TODO Auto-generated method stub
//			lb_check = isChecked;
////			fcsa.updateBtnEnable();
//		}
//	};
	
	
	public final class ViewHolder{
		public TextView tv_item;
		public TextView tv_des;
		public ImageButton ibtn_delete;
		public LinearLayout ll_no;
		public LinearLayout ll_delete;
	}
	
	public void ScrollToRow(int row){
		selectedRow = row;
		this.notifyDataSetChanged();
	}
	public int GetSelectedIndex(){
		return selectedRow;
	}

	public void ShowMessage(int style,int icon,String ls_msg)
	{
		if(mMessageDialog != null && mMessageDialog.isShowing()){
			mMessageDialog.dismiss();
		}
		mMessageDialog = new MessageDialog(fcsa);
		mMessageDialog.setStyle(style);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(ls_msg);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}
	
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener(){

		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub
			ImageButton ib = (ImageButton) ImageButtonView.findViewById(R.id.ibtn_delete);
//			
			String upc = ib.getTag().toString();
			fcsa.DeleteMiaMatch(upc);
			mMessageDialog.dismiss();
		}

		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub
			
			mDeviceManager.clearAllScannerBuffer();
			mDeviceManager.enableAllScanner();
			mMessageDialog.dismiss();
		}

		@Override
		public void onClickOK() {
			// TODO Auto-generated method stub
			
			mDeviceManager.clearAllScannerBuffer();
			mDeviceManager.enableAllScanner();
			mMessageDialog.dismiss();
		}
		
	};
}
